package com.qys.livesMall.system.service.impl;

import com.qys.livesMall.cache.words.source.WordCache;
import com.qys.livesMall.common.exception.enums.ExceptionEnum;
import com.qys.livesMall.common.message.MessageServer;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.PatternUtil;
import com.qys.livesMall.framework.view.Paging;
import com.qys.livesMall.framework.view.QysPage;
import com.qys.livesMall.system.enums.dao.SysSensitiveVocabularyDao;
import com.qys.livesMall.system.enums.SystemMessageEnum;
import com.qys.livesMall.system.model.dto.words.EditWords;
import com.qys.livesMall.system.model.dto.words.WordsSearch;
import com.qys.livesMall.system.model.entity.SysSensitiveVocabulary;
import com.qys.livesMall.system.mapper.SysSensitiveVocabularyMapper;
import com.qys.livesMall.system.model.vo.sys.WordsVO;
import com.qys.livesMall.system.service.SysSensitiveVocabularyService;
import jakarta.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 敏感词库 服务实现类
 * </p>
 *
 * @author
 * @since 2023-06-08
 */
@Service
public class SysSensitiveVocabularyServiceImpl  implements SysSensitiveVocabularyService {

    @Autowired
    private SysSensitiveVocabularyDao sysSensitiveVocabularyDao;
    @Autowired
    private SysSensitiveVocabularyMapper sensitiveVocabularyMapper;

    @Autowired
    private MessageServer messageServer;

    @PostConstruct
    public void init() {
        //加载至环境
        List<SysSensitiveVocabulary> list= sysSensitiveVocabularyDao.list();
        if (CollectionUtils.isEmpty(list)){
            return;
        }
        WordCache.wordList=list.stream().map(SysSensitiveVocabulary::getWords).collect(Collectors.toList());
    }
    @Override
    public QysPage<WordsVO> list(WordsSearch search) {
        Paging<SysSensitiveVocabulary> page = sensitiveVocabularyMapper.search(new Paging<>(search));
        return QysPage.to(page,WordsVO.class);
    }

    public R add(EditWords editWords){
        if (editWords==null||StringUtils.isBlank(editWords.getWords())){
            return messageServer.failMsg(ExceptionEnum.PARAM_IS_ERROR);
        }
        List<String> words= Arrays.stream(editWords.getWords().split(",")).filter(StringUtils::isNotBlank).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(words)){
            return R.ok();
        }
        List<SysSensitiveVocabulary> svs=new ArrayList<>();

        for(String word:words){
            if(PatternUtil.matcherNumber(word)){
                return messageServer.failMsg(SystemMessageEnum.NUMBERS_NOT_SENSITIVE_WORDS);
            }
            SysSensitiveVocabulary sv=new SysSensitiveVocabulary();
            sv.setLang(editWords.getLang());
            sv.setWords(word);
            svs.add(sv);
        }

        sysSensitiveVocabularyDao.saveBatch(svs);
        WordCache.wordList.addAll(words);
        return R.ok();
    }

    public R edit(EditWords editWords){
        if (editWords==null||StringUtils.isBlank(editWords.getWords())){
            return messageServer.failMsg(ExceptionEnum.PARAM_IS_ERROR);
        }
        SysSensitiveVocabulary sv = sysSensitiveVocabularyDao.getById(editWords.getId());
        if (sv==null){
            return messageServer.failMsg(ExceptionEnum.PARAM_IS_ERROR);
        }
        if(PatternUtil.matcherNumber(editWords.getWords())){
            return messageServer.failMsg(SystemMessageEnum.NUMBERS_NOT_SENSITIVE_WORDS);
        }
        sv.setWords(editWords.getWords());
        sv.setLang(editWords.getLang());
        sysSensitiveVocabularyDao.updateById(sv);
        init();
        return R.ok();
    }

    public R del(List<Long> ids) {
        sysSensitiveVocabularyDao.removeBatchByIds(ids);
        init();
        return R.ok();
    }
}
